<?php
/**
 * 开店星新零售管理系统
 * @description 基于Yii2+Vue2.0+uniapp研发，H5+小程序+公众号全渠道覆盖，功能完善开箱即用，框架成熟易扩展二开
 * @author 青岛开店星信息技术有限公司
 * @link https://www.kaidianxing.com
 * @copyright Copyright (c) 2020-2022 Qingdao ShopStar Information Technology Co., Ltd.
 * @copyright 版权归青岛开店星信息技术有限公司所有
 * @warning Unauthorized deletion of copyright information is prohibited.
 * @warning 未经许可禁止私自删除版权信息
 */

namespace shopstar\components\dispatch\driver;

use shopstar\components\dispatch\bases\BaseDispatchDriver;
use shopstar\components\dispatch\bases\DispatchDriverInterface;
use yii\helpers\Json;

/**
 * 顺丰驱动类
 * Class SfDriver
 * @package shopstar\components\dispatch\driver
 * @author 青岛开店星信息技术有限公司
 */
class SfDriver extends BaseDispatchDriver implements DispatchDriverInterface
{

    /**
     * 请求接口
     * @var string
     */
    private $url = 'http://commit-openic.sf-express.com';

    /**
     * 下单地址
     * @var string
     */
    private $apiAddOrder = '/open/api/external/createorder?sign=';

    /**
     * 取消订单地址
     * @var string
     */
    private $apiCancelOrder = '/open/api/external/cancelorder?sign=';

    private $apiQueryStatus = '/open/api/external/riderlatestposition?sign=';

    public $dev_id;

    public $dev_key;

    public function connect()
    {
        parent::connect(); // TODO: Change the autogenerated stub

    }

    /**
     * 新增订单
     * @param $data
     * @return array|mixed
     * @author 青岛开店星信息技术有限公司
     */
    public function addOrder($data)
    {
        return self::getResult($this->apiAddOrder, $data);
    }


    public function queryStatus($orderId)
    {
        $data = [
            'order_id' => $orderId,
            'order_type' => 1,
            'push_time' => time()
        ];

        return self::getResult($this->apiQueryStatus, $data);
    }

    /**
     * 取消订单
     * @param array $data
     * @return array|mixed
     * @author 青岛开店星信息技术有限公司
     */
    public function cancelOrder(array $data)
    {
        return self::getResult($this->apiCancelOrder, $data);
    }


    /**
     * 实现请求
     * @param string $api
     * @param array $data
     * @return array|mixed
     * @author 青岛开店星信息技术有限公司
     */
    private function getResult(string $api, array $data)
    {
        $params = self::getParams($data);

        $sign = self::sign($params);

        $url = $this->url . $api . $sign;

        $response = self::http_post($url, $params);

        $response = Json::decode($response);

        if ($response['error_code'] == 0 && $response['error_data'] == null) {

            return $response['result'];

        } else {
            if ($response['code'] == 3006) {
                return error('超出配送范围');
            }
            return error($response['error_msg']);
        }
    }

    /**
     * 拼装数据转JSON
     * @param $data
     * @return false|string
     * @author 青岛开店星信息技术有限公司
     */
    private function getParams(array $data)
    {
        $data['dev_id'] = $this->dev_id;
        $postData = json_encode($data);

        return $postData;
    }

    /**
     * 签名方法
     * @param $data
     * @return string
     * @author 青岛开店星信息技术有限公司
     */
    private function sign($data)
    {
        $signChar = $data . "&{$this->dev_id}&{$this->dev_key}";

        $signChar = base64_encode(MD5($signChar));

        return $signChar;
    }

    /**
     * 携带参数请求
     * @param $url
     * @param $jsonData
     * @return bool|string
     * @author 青岛开店星信息技术有限公司
     */
    private function http_post(string $url, $jsonData)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        $output = curl_exec($ch);
        curl_close($ch);

        return $output;
    }

}